﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Write.Api.Handler.RBAC
{
    public class BackUserAndRoleCommandHandler : IRequestHandler<BackfillUserFromRoleCommand, ApiResult>
    {
        private readonly ILogger<BackUserAndRoleCommandHandler> _logger;

        private readonly IUserRepository _userRepository;

        private readonly IRoleRepository _roleRepository;

        private readonly IMapper _mapper;
        public BackUserAndRoleCommandHandler(ILogger<BackUserAndRoleCommandHandler> logger, IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper)
        {
            _logger = logger;
            _userRepository = userRepository;
            _roleRepository = roleRepository;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(BackfillUserFromRoleCommand request, CancellationToken cancellationToken)
        {
            try
            {                
                var users = await _userRepository.GetAsync(x=>x.UserId==request.UserId);
                if (users.UserId==null)
                {
                    return ApiResult.Fail("回显失败");
                }

                return ApiResult.Ok(users);
                
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error in BackUserAndRoleCommandHandler");
                return  ApiResult.Fail("Error in BackUserAndRoleCommandHandler");
            }
        }
    }
}
public class BackfillUserFromRoleCommand:IRequest<ApiResult>
{
    public int UserId { get; set; }
}